<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>vue cli 3.x 项目部署到 github pages - Lucklyの博客 - 在阅读中遇见自己</title>
    <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Luckly" /><meta name="description" content="github pages 是 github 免费为用户提供的服务，写博客，或者部署一些纯静态项目。
最近将 vue cli 3.x 初始化项目部署到 github pages，踩了一些坑，记录如下。
" /><meta name="keywords" content="Hugo, theme, even" />






<meta name="generator" content="Hugo 0.79.1 with even 4.0.0" />


<link rel="canonical" href="https://luckly.work/2019/04/vue-cli-3.x-%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2%E5%88%B0-github-pages/" />
<link href="/2019/04/vue-cli-3.x-%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2%E5%88%B0-github-pages/" rel="alternate" type="application/rss+xml" title="Lucklyの博客 - 在阅读中遇见自己" />
<link href="/2019/04/vue-cli-3.x-%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2%E5%88%B0-github-pages/" rel="feed" type="application/rss+xml" title="Lucklyの博客 - 在阅读中遇见自己" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<link href="/dist/even.06658218.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/reset-even.css">


<meta property="og:title" content="vue cli 3.x 项目部署到 github pages" />
<meta property="og:description" content="github pages 是 github 免费为用户提供的服务，写博客，或者部署一些纯静态项目。
最近将 vue cli 3.x 初始化项目部署到 github pages，踩了一些坑，记录如下。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://luckly.work/2019/04/vue-cli-3.x-%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2%E5%88%B0-github-pages/" />
<meta property="article:published_time" content="2019-04-16T21:20:39+08:00" />
<meta property="article:modified_time" content="2019-04-16T21:20:39+08:00" />
<meta itemprop="name" content="vue cli 3.x 项目部署到 github pages">
<meta itemprop="description" content="github pages 是 github 免费为用户提供的服务，写博客，或者部署一些纯静态项目。
最近将 vue cli 3.x 初始化项目部署到 github pages，踩了一些坑，记录如下。">
<meta itemprop="datePublished" content="2019-04-16T21:20:39+08:00" />
<meta itemprop="dateModified" content="2019-04-16T21:20:39+08:00" />
<meta itemprop="wordCount" content="738">



<meta itemprop="keywords" content="Vue,GitHub Pages," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="vue cli 3.x 项目部署到 github pages"/>
<meta name="twitter:description" content="github pages 是 github 免费为用户提供的服务，写博客，或者部署一些纯静态项目。
最近将 vue cli 3.x 初始化项目部署到 github pages，踩了一些坑，记录如下。"/>

</head>
<body>
<div id="mobile-navbar" class="mobile-navbar">
    <div class="mobile-header-logo">
        <a href="/" class="logo">遇见</a>
    </div>
    <div class="mobile-navbar-icon">
        <span></span>
        <span></span>
        <span></span>
    </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
    <ul class="mobile-menu-list">
        <a href="/">
            <li class="mobile-menu-item" title="首页">首页</li>
        </a>
        <a href="/post/">
            <li class="mobile-menu-item" title="归档">归档</li>
        </a>
        <a href="/categories/">
            <li class="mobile-menu-item" title="分类">分类</li>
        </a>
        <a href="/tags/">
            <li class="mobile-menu-item" title="标签">标签</li>
        </a>
        <a href="/about/">
            <li class="mobile-menu-item" title="关于我">关于我</li>
        </a>
        <a href="/friend/">
            <li class="mobile-menu-item" title="友链">友链</li>
        </a>
        <a href="https://github.com/ITmxs/">
            <li class="mobile-menu-item" title="阅读清单">阅读清单</li>
        </a>
        
    </ul>
    <form class="search-form" style="position: absolute;">
    <input name="search" type="search" style="height: 28px;padding-left: 30px;"/>
    <input type="image" src="/img/search.svg" alt="Search" style="position: absolute;left: 6px;top: 6px;"/>
</form>


</nav>

<div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/%20/" class="logo">遇见</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/">首页</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/post/">归档</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/">分类</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/tags/">标签</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于我</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/friend/">友链</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="https://github.com/ITmxs/">阅读清单</a>
    </li>
  </ul>
</nav>
        <form class="search-form" style="position: absolute;">
    <input name="search" type="search" style="height: 28px;padding-left: 30px;"/>
    <input type="image" src="/img/search.svg" alt="Search" style="position: absolute;left: 6px;top: 6px;"/>
</form>



    </header>


    <main id="main" class="main">
        <div class="content-wrapper">
            <div id="content" class="content">
                <article class="post">
  
  <header class="post-header">
    <h1 class="post-title">vue cli 3.x 项目部署到 github pages</h1>

    <div class="post-meta">
      <span class="post-time"> 2019-04-16 </span>
      <div class="post-category">
        <a href="/categories/%E6%8A%80%E6%9C%AF/"> 技术 </a>
        </div>
      <span class="more-meta"> 约 738 字 </span>
      <span class="more-meta"> 预计阅读 2 分钟 </span>
      <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
    </div>
  </header>

  <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-vue-router-不要开启-history-模式">1. vue-router 不要开启 history 模式</a></li>
        <li><a href="#2-配置-publicurl">2. 配置 <strong>publicUrl</strong></a></li>
        <li><a href="#3-css-引入图片错误">3. css 引入图片错误</a></li>
        <li><a href="#4-自动部署脚本">4. 自动部署脚本</a></li>
        <li><a href="#开启-github-pages">开启 github pages</a></li>
        <li><a href="#代码">代码</a></li>
        <li><a href="#参考">参考</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
  <div class="post-content">
    <p>github pages 是 github 免费为用户提供的服务，写博客，或者部署一些纯静态项目。</p>
<p>最近将 vue cli 3.x 初始化项目部署到 github pages，踩了一些坑，记录如下。</p>
<h2 id="1-vue-router-不要开启-history-模式">1. vue-router 不要开启 history 模式</h2>
<p>路径中的 <strong>#</strong> 比较丑，就开启了 vue-router 的 history 模式，去掉了 <strong>#</strong>。平时做项目也是默认开启 history 模式。折腾了半天发现，我这是部署到 github pages ，服务器没有配置，导致页面一直请求不到。</p>
<h2 id="2-配置-publicurl">2. 配置 <strong>publicUrl</strong></h2>
<p>打包路径也要单独配置，否则也是请求不到页面。</p>
<p>比如我的项目地址是 <code>https://github.com/nusr/resume-vue</code></p>
<p>我要部署到 <code>https://nusr.github.io/resume-vue</code>，那么 <strong>publicPath</strong> 要配置为 <strong>/resume-vue</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="c1">// vue.config.js
</span><span class="c1"></span><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">publicPath</span><span class="o">:</span> <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">NODE_ENV</span> <span class="o">===</span> <span class="s2">&#34;production&#34;</span> <span class="o">?</span> <span class="s2">&#34;/resume-vue&#34;</span> <span class="o">:</span> <span class="s2">&#34;/&#34;</span>
<span class="p">};</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="3-css-引入图片错误">3. css 引入图片错误</h2>
<p>css 引入背景图片时，开发环境没有任何问题，但是一旦部署后，就无法获取图片了。</p>
<p>稍微改下 <strong>App.vue</strong> 的代码，暂时解决了这个问题。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-html" data-lang="html"><span class="c">&lt;!--App.vue--&gt;</span>
<span class="p">&lt;</span><span class="nt">template</span><span class="p">&gt;</span>
  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&#34;app&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">router-view</span> <span class="p">/&gt;</span>
  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">template</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
  <span class="kr">export</span> <span class="k">default</span> <span class="p">{</span>
    <span class="nx">name</span><span class="o">:</span> <span class="s2">&#34;App&#34;</span><span class="p">,</span>
    <span class="nx">mounted</span><span class="p">()</span> <span class="p">{</span>
      <span class="cm">/**
</span><span class="cm">       * 解决 css 引入图片在 github pages 无法获取的问题
</span><span class="cm">       */</span>
      <span class="kr">const</span> <span class="p">{</span> <span class="nx">NODE_ENV</span> <span class="p">}</span> <span class="o">=</span> <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">;</span>
      <span class="nb">document</span><span class="p">.</span><span class="nx">documentElement</span><span class="p">.</span><span class="nx">className</span> <span class="o">=</span> <span class="nx">NODE_ENV</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">};</span>
<span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">style</span> <span class="na">lang</span><span class="o">=</span><span class="s">&#34;less&#34;</span><span class="p">&gt;</span>
  <span class="p">@</span><span class="k">import</span> <span class="s2">&#34;~@/assets/global.less&#34;</span><span class="p">;</span>
<span class="p">&lt;/</span><span class="nt">style</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>给 <strong>html</strong> 标签配置了一个顶级类，写上不同的 css 解决了这个问题。</p>
<p>真时的部署环境不是这样的，部署文件夹就是根目录，但 github pages 部署文件夹不是根目录，就有这个问题。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-css" data-lang="css"><span class="o">//</span> <span class="nt">global</span><span class="p">.</span><span class="nc">less</span>
<span class="p">.</span><span class="nc">development</span> <span class="p">{</span>
  <span class="k">background-image</span><span class="p">:</span> <span class="nb">url</span><span class="p">(</span><span class="sx">/background.png</span><span class="p">);</span>
<span class="p">}</span>

<span class="p">.</span><span class="nc">production</span> <span class="p">{</span>
  <span class="k">background-image</span><span class="p">:</span> <span class="nb">url</span><span class="p">(</span><span class="sx">/resume-vue/background.png</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="4-自动部署脚本">4. 自动部署脚本</h2>
<p>根目录下新建 <strong>deploy.sh</strong> 文件，文件内容如下。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># deploy.sh</span>
<span class="c1"># 错误时停止</span>
<span class="nb">set</span> -e

<span class="c1"># 打包</span>
npm run build

<span class="c1"># 进入目标文件夹</span>
<span class="nb">cd</span> dist

<span class="c1"># 提交到本地仓库</span>

git init
git add -A
git commit -m <span class="s1">&#39;deploy&#39;</span>

<span class="c1"># 提交到 https://github.com:nusr/resume-vue 项目的 gh-pages 分支</span>
git push -f git@github.com:nusr/resume-vue.git master:gh-pages

<span class="nb">cd</span> -
</code></pre></td></tr></table>
</div>
</div><p>部署命令是 <code>bash deploy.sh</code></p>
<h2 id="开启-github-pages">开启 github pages</h2>
<p>建立仓库，仓库名称是 <strong>username.github.io</strong> ,必须是这种格式。</p>
<p>比如我的用户名是 <strong>nusr</strong>,建立的仓库就是 <strong>nusr.github.io</strong>。</p>
<p>github pages 默认开启分支是 <strong>gh-pages</strong>,可以进入该仓库页面，点击 <strong>Settings -&gt; GitHub Pages</strong>,切换展示的分支。</p>
<p>GitHub Pages 支持定制域名,支持 <strong>jsonp</strong> 请求。</p>
<h2 id="代码">代码</h2>
<p><a href="https://github.com/nusr/resume-vue">代码地址</a>
<a href="https://nusr.github.io/resume-vue/">预览地址</a></p>
<h2 id="参考">参考</h2>
<ol>
<li><a href="https://pages.github.com/">github pages 官方说明</a></li>
<li><a href="https://cli.vuejs.org/zh/guide/deployment.html#%E9%80%9A%E7%94%A8%E6%8C%87%E5%8D%97">vue cli 3.x 部署官方说明</a></li>
</ol>
  </div>

  <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Luckly</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2019-04-16
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
    <div class="post-tags">
      <a href="/tags/vue/">Vue</a>
      <a href="/tags/github-pages/">GitHub Pages</a>
      </div>
    <nav class="post-nav">
      <a class="prev" href="/2019/04/%E4%B8%80%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9B%B4%E6%96%B0%E6%89%80%E6%9C%89-npm-%E4%BE%9D%E8%B5%96%E5%8C%85/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">一行命令更新所有 npm 依赖包</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
      <a class="next" href="/2019/04/%E8%A7%A3%E5%86%B3-mac-homebrew-%E4%B8%8B%E8%BD%BD%E7%BC%93%E6%85%A2%E7%9A%84%E9%97%AE%E9%A2%98/">
        <span class="next-text nav-default">解决 mac HomeBrew 下载缓慢的问题</span>
        <span class="next-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    </nav>
  </footer>
  
</article>
            </div>
            

        </div>
    </main>

    <footer id="footer" class="footer">
        <div class="social-links">
  <a href="852851198@qq.com" class="iconfont icon-email" title="email" rel="noopener" target="_blank" data-title="email"> </a>
  <a href="https://github.com/ITmxs" class="iconfont icon-github" title="github" rel="noopener" target="_blank" data-title="github"> </a>
  <a href="https://juejin.cn/user/3843548384077192" class="iconfont icon-juejin" title="juejin" rel="noopener" target="_blank" data-title="juejin"> </a>
  <a href="https://www.zhihu.com/people/yimi-yang-guang-96-65" class="iconfont icon-zhihu" title="zhihu" rel="noopener" target="_blank" data-title="zhihu"> </a>
  <a href="https://space.bilibili.com/480883651" class="iconfont icon-bilibili" title="bilibili" rel="noopener" target="_blank" data-title="bilibili"> </a>
  <a href="https://luckly.work/index.xml" type="application/rss+xml" rel="noopener" target="_blank" class="iconfont icon-rss" title="rss"></a>


</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy;
    2020 -
    2021
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">Luckly</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
    </div>
</div>
<script src="/lib/highlight/highlight.pack.js?v=20171001"></script>
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>
<script type="text/javascript" src="/dist/even.47f727f4.min.js"></script>








</body>
</html>
